<?php

require_once 'phpmoko/ui/Panel.php';

class RootPanel extends Panel
{
	private static $uniqueId = 0;
	private static $instance = NULL;
	private $title;
	private $links = array();
	private $linksFixed = array();
	private $jsFiles = array();
	private $jsFilesFixed = array();
	private $metaTags = array();
	private $jsCode = null;
	private $onReadyJs = null;
	private $cache = true;
	private $showLogo = true;
	private $googleAnalyticsId = null;
	private $favicon = null;
	private $combiFiles = false;
	private $mysql = null;

	protected function __construct()
	{
		parent::__construct();
	}

	function setMySQL($mysql)
	{
		$this->mysql = $mysql;
	}
	
	/**
	 * @return MySQL
	 */
	function getMySQL()
	{
		if (!$this->mysql)
			die('Mysql not defined');

		return $this->mysql;
	}
	
	function setGoogleAnalyticsId($id)
	{
		$this->googleAnalyticsId = $id;
	}

	function setShowLogo($enabled)
	{
		$this->showLogo = $enabled;
	}
	
	function setCombiFiles($enabled)
	{
		$this->combiFiles = $enabled;
	}
	
	static function getUniqueId()
	{
		self::$uniqueId++;
		return 'single_'.self::$uniqueId;
	}

	/**
	 * gets the RootPanel
	 * @return RootPanel The current object
	 */
	static function get()
	{
		if (self::$instance == NULL)
			self::$instance = new RootPanel();

		return self::$instance;
	}

	function addCSSFile($href, $fixed = false)
	{
		include_once 'phpmoko/ui/Head/Link.php';

		$link = new Link();
		$link->setRel('stylesheet');
		$link->setType('text/css');
		$link->setHref($href);
		
		if ($fixed)
			$this->linksFixed[] = $link;
		else
			$this->links[] = $link;
	}

	function setFavicon($href)
	{
		include_once 'phpmoko/ui/Head/Link.php';

		$this->favicon = new Link();
		$this->favicon->setRel('SHORTCUT ICON');
		$this->favicon->setHref($href);
	}

	function addJSFile($file, $fixed = false)
	{
		if ($fixed)
			$this->jsFilesFixed[] = $file;
		else
			$this->jsFiles[] = $file;
	}

	function addJSCode($code)
	{
		if ($this->jsCode != null)
			$this->jsCode .= ' ';

		$this->jsCode .= $code;
	}

	function addJSOnReadyDocument($js)
	{
		if ($this->onReadyJs != null)
			$this->onReadyJs .= ' ';
			
		$this->onReadyJs .= $js;
	}

	function setTitle($title)
	{
		$this->title = $title;
	}

	function setMetaTag($name, $content)
	{
		$this->metaTags[$name] = $content;
	}
	
	function enableCache($enable)
	{
		$this->cache = $enable;
	}
	
	function toString()
	{
		$content = '';
		foreach ($this as $widget)
			$content .= $widget->toString();

		$str = '<?xml version="1.0" encoding="utf-8">';

		$str .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

		$str .= '<html>';

		$str .= '<head>';
		if ($this->title)
			$str .= '<title>'.$this->title.'</title>';

		foreach ($this->metaTags as $name => $value)
			$str .= '<meta name="'.$name.'" content="'.$value.'" />';

		if ($this->favicon != null)
			$str .= $this->favicon->toString();

		$cssfiles = "";
		for ($i = 0; $i < sizeof($this->links); $i++)
		{
			if ($this->combiFiles && ($this->links[$i]->getType() == "text/css"))
			{
				if ($cssfiles != "")
					$cssfiles .= "&";

				$cssfiles .= $this->links[$i]->getHref();
			}
			else
				$str .= $this->links[$i]->toString();
		}
		
		if ($this->combiFiles && ($cssfiles != ""))
		{
			$link = new Link();
			$link->setRel('stylesheet');
			$link->setType('text/css');
			$link->setHref('/a/combi?'.$cssfiles.'&id='.date('Ymd'));
			$str .= $link->toString();
		}
		
		for ($i = 0; $i < sizeof($this->linksFixed); $i++)
			$str .= $this->linksFixed[$i]->toString();

		$jsfiles = "";
		for ($i = 0; $i < sizeof($this->jsFiles); $i++)
		{
			if ($this->combiFiles)
			{
				if ($jsfiles != "")
					$jsfiles .= "&";

				$jsfiles .= $this->jsFiles[$i];
			}
			else
				$str .= '<script src="'.$this->jsFiles[$i].'" type="text/javascript"></script>';
		}

		if ($this->combiFiles && ($jsfiles != ""))
			$str .= '<script src="/a/combi?'.$jsfiles.'&id='.date('Ymd').'" type="text/javascript"></script>';

		for ($i = 0; $i < sizeof($this->jsFilesFixed); $i++)
		{
			$str .= '<script src="'.$this->jsFilesFixed[$i].'" type="text/javascript"></script>';
		}

		if ($this->jsCode != null)
			$str .= '<script type="text/javascript">'.$this->jsCode.'</script>';
			
		$str .= '</head>';
		$str .= '<body leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">';

		if ($this->googleAnalyticsId != null)
			$str .= "<script type=\"text/javascript\">\nvar _gaq = _gaq || [];\n_gaq.push(['_setAccount', '".$this->googleAnalyticsId."']);\n_gaq.push(['_trackPageview']);\n(function() {\nvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\nga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);\n})();</script>";

		$str .= $content;

		if ($this->onReadyJs != null)
			$str .= '<script> $(document).ready(function() { '.$this->onReadyJs.' });</script>';

		if ($this->showLogo)
			$str .= '<center><a href="http://www.phpmoko.com/"><img src="http://www.phpmoko.com/images/powered_by_phpmoko.png" border="0"></a></center>';
		
		$str .= '</body>';
		$str .= '</html>';

		if ($this->cache)
		{
			require_once 'phpmoko/utils/Cache.php';
			$str = Cache::compressStream($str);
		}

		return $str;
	}
}
?>